9 API Auth Implementation
Here is a safe dual-auth rollout you can execute in this repository.
Implementation Plan
- Add AD token validation in OWIN while keeping NTLM active.
- Update identity extraction to prefer bearer-token claims and fallback to existing Windows identity.
- Add feature flags so you can switch behavior without redeploying code.
- Update SignalR auth path to use the same identity resolver.
- Pilot and cut over, then disable NTLM.
Files To Change First
- Startup.cs
Add bearer-token middleware before WebApi middleware. - AppAuthorizationMessageHandler.cs
Replace hard dependency on HttpContext Windows identity with a resolver that checks token claims first. - SignalrAuthorizationAttribute.cs
Use the same resolver to read identity from SignalR token/cookies. - Web.config:630 Add new auth flags and Entra settings; later switch auth mode after cutover.
Config You Should Add
In appSettings:
- Api.Settings.Auth.Mode = Dual | TokenOnly | WindowsOnly
- Api.Settings.Auth.Entra.TenantId
- Api.Settings.Auth.Entra.Audience
- Api.Settings.Auth.Entra.ValidIssuers
- Api.Settings.Auth.Claims.UsernamePriority = preferred_username;upn;email
- Api.Settings.Auth.EnableWindowsFallback = true initially
Identity Resolution Logic
Create one resolver service and use it from both API handler and SignalR:
- If bearer token exists and is valid:
- Read username claim by priority: preferred_username, then upn, then email.
- Optionally map UPN to DOMAINif your PrincipalUserQuery expects that.
- Else if Windows principal exists:
- Use existing HttpContext.Current.User.Identity.Name behavior.
- Else:
- Return 401/403 as now.
Then keep current PrincipalUserQuery path unchanged so authorization/business rules remain stable.
Middleware Order (important)
In Startup:
- Use token auth middleware first.
- Then UseWebApi.
- Keep your existing delegating handlers.
This ensures claims principal is available before AppAuthorizationMessageHandler runs.
SignalR Path
For SignalR:
- Accept access token in query/header (depending on client transport support).
- Resolve identity using same claim priority.
- Keep impersonation query parameter behavior unchanged.
Rollout Strategy
- Stage 1 (Dual mode):
- Windows auth remains on.
- Bearer accepted.
- Log identity source (Token vs Windows).
- Stage 2 (Pilot users/departments):
- Force selected front-end clients to send bearer tokens.
- Monitor 401/403 and user-mapping misses.
- Stage 3 (Token-only):
- Switch Api.Settings.Auth.Mode to TokenOnly.
- Enable anonymous at IIS and disable Windows auth at host level.
- Stage 4 (Cleanup):
- Remove NTLM-only branches after 2-4 weeks stable.
Validation Checklist
- Existing desktop domain user opens app and gets in with no login prompt.
- Token user maps to same PrincipalUser as old NTLM path.
- Impersonation header still works.
- Webhooks/system URLs continue to use system account path.
- SignalR hub authorization still succeeds.
- External app and certificate-protected endpoints still pass.
Risk Controls
- Keep EnableWindowsFallback true until mapping is proven.
- Add audit logs for username source and mapped principal id.
- Add feature flag kill switch to revert to WindowsOnly quickly.